package com.lhkj.ct.admin.modules.gauge.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblGaugeCategory;
import com.lhkj.ct.meta.modules.gauge.service.GaugeCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 量表/问卷类别 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-10-08
 */
@RestController
@RequestMapping("api/plat/gauge/category")
@Validated
@Api(tags = "6.4测量分类")
public class GaugeCategoryController {

    @Resource
    private GaugeCategoryService categoryService;

    @GetMapping(value = "list")
    @ApiOperation(value = "列表接口")
    @ApiOperationSupport(order = 1)
    public ActionResult<IPage<TblGaugeCategory>> list(TblGaugeCategory entity, PageQuery pageQuery) {
        return ActionResult.success(categoryService.selectCategoryPage(entity, pageQuery));
    }

    @GetMapping(value = "options")
    @ApiOperation(value = "下拉选项分页接口")
    @ApiParam(name="cType",value="类型 1.量表 2.问卷",allowableValues = "1,2",required=true)
    public ActionResult<IPage<Option<String>>> selectCategoryOptions(@Validated(ValGroups.Get.class) TblGaugeCategory category, PageQuery pageQuery) {
        return ActionResult.success( categoryService.selectCategoryOptions(category, pageQuery));
    }

    @GetMapping(value = "checkName")
    @ApiOperation(value = "校验名称是否唯一")
    @ApiOperationSupport(order = 2)
    public ActionResult<Boolean> checkName(TblGaugeCategory category) {
        return ActionResult.success(categoryService.checkNameUnique(category));
    }

    @GetMapping(value = "{cId}/get")
    @ApiOperation(value = "表单数据")
    @ApiOperationSupport(order = 3)
    public ActionResult<TblGaugeCategory> get(@PathVariable String cId) {
        return ActionResult.success(categoryService.getById(cId));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增")
    @PreventDuplicateSubmit
    @PostMapping(value = "add")
    @Log(title = "测量分类", actionType = ActionType.INSERT)
    public ActionResult<TblGaugeCategory> add(@Validated(ValGroups.Insert.class) @RequestBody TblGaugeCategory category) {
        Asserts.assertTrue(categoryService.checkNameUnique(category), "分类名称已存在");
        categoryService.insert(category);
        return ActionResult.success(category);
    }

    @PutMapping(value = "update")
    @ApiOperation(value = "修改")
    @ApiOperationSupport(order = 5)
    @Log(title = "测量分类", actionType = ActionType.UPDATE)
    public ActionResult<Void> update(@Validated({ValGroups.Update.class}) @RequestBody TblGaugeCategory entity) {
        TblGaugeCategory target = categoryService.getById(entity.getId());
        Asserts.assertFalse(null == target, "分类不存在");
        Asserts.assertTrue(categoryService.checkNameUnique(entity), "分类名称已存在");
        return ActionResult.judge(categoryService.updateById(entity));
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除", notes = "支持批量删除")
    @ApiOperationSupport(order = 6)
    @Log(title = "测量分类", actionType = ActionType.DELETE)
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "删除的编号不能为空") List<String> ids) {
        return ActionResult.success("删除完成，共删除【" + categoryService.removeByIds(ids) + "】条");
    }

}
